import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> heap =new PriorityQueue<>((a,b)->b-a);
        for(int x:stones){
            heap.add(x);
        }
        while (heap.size()>1){
            int x=heap.poll();
            int y=heap.poll();
            if(x-y>0){
                heap.add(x-y);
            }
        }
        return heap.size()==0?0:heap.poll();
    }
}